﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：WarehousingImpl
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/14 15:13:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion


using K8.MDCBB;
using KingerRobot.MexicoSailunWMS.Spi;
//using MyBatis.DataMapper.TypeHandlers;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Cryptography;

namespace KingerRobot.MexicoSailunWMS.SpiService;

public class BProductImpl : IProductService
{

    private readonly static IProdProduct _ProProduct = new BProdProduct();
    private readonly static IProdMaterial _ProMaterial = new BProdMaterial();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IRcsEquipBuilding _RcsEquipBuilding = new BRcsEquipBuilding();
    private readonly static IBakWcsTask _BakWcsTask = new BBakWcsTask();
    private readonly static IProdSku _ProdSku = new BProdSku();
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    public List<ProductInfo> GetProdProductByCode(ProductParm productParm)
    {

        var code = productParm.Code;

        var result = _ProProduct.GetProductListByCodeV1(code, productParm?.orderByName, productParm?.orderByType);
        //if (!string.IsNullOrWhiteSpace(productParm?.orderByName))
        //{


        //    //result = result.OrderBy(t => t.GetType().GetProperty("materialCode").GetValue(t,null)).ToList();

        //    //var s = from p in result
        //    //         orderby GetPropertyValue(p, "materialCode")
        //    //         select p;
        //    //result = s.ToList();
        //}

        return result;
      
        ////获取所有成型机设备
        //var buildingIds = _RcsEquipBuilding.GetAllEquipBuildings().Select(t => t.Id).ToList();
        //var equipList = _RcsEquip.GetRcsEquipByIds(buildingIds);

        //result = productList.GroupJoin(equipList, f => f.MdEquipId, s => s.Id, (f, s) => new
        //{
        //    product = f,
        //    equip = s,
        //}).SelectMany(f => f.equip.DefaultIfEmpty(), (f, s) => new
        //{
        //    product = f.product,
        //    equipIdNeme = s?.EquipName ?? "",
        //}).Join(materList, f => f.product.MaterialId, s => s.Id, (f, s) => new ProductInfo
        //{
        //    code = f.product.ProductCode,
        //    materialCode = s.MaterialCode,
        //    materialName = s.MaterialName,
        //    materialSpec = s.Spec,
        //    equipName = f?.equipIdNeme ?? "",
        //    mdTime = f?.product.MdTime ?? null,
        //}).OrderByDescending(t => t.mdTime).ToList();

    }




  

    public List<ScanerBandInfo> GetBandInfoByEquipName(BindInfoParm bindParm)
    {
        var equipName = bindParm.EquipName;
        var equip = _RcsEquip.GetRcsEquipByName(equipName);
        if (equip == null)
        {
            return null;
        }
        var bacWcsTasks = _BakWcsTask.GetBakWcsTasksByStartEquip(equip.Id.GetValueOrDefault());
        var maters = _ProMaterial.GetMaterialList();
        var skus = _ProdSku.GetProdSkuList();
        var taskMaters = bacWcsTasks.Join(skus, f => f.SkuId, s => s.Id, (f, s) => new ScanerBandInfo
        {
            code = s.SkuCode,
            materialId = s.MaterialId.GetValueOrDefault(),
            taskNo = f.TaskNo.GetValueOrDefault(),
            targetLoc = f.TargetLoc.GetValueOrDefault(),
            createTime = f.CreateTime.GetValueOrDefault()
        }).Join(maters, f => f.materialId, s => s.Id, (f, s) => new ScanerBandInfo
        {
            code = f.code,
            materialId = f.materialId,
            taskNo = f.taskNo,
            targetLoc = f.targetLoc,
            createTime = f.createTime,
            materialCode = s.MaterialCode,
            materialName = s.MaterialName,
            materialSpec = s.Spec
        }).ToList();
        return taskMaters;
    }

    public List<StockMdTimeCount> GetStockMdTimeCount()
    {
        var result = _WcsLocSku.GetStockMdTimeCount();
        return result;
    }
}






